import { ZCC_TYPE } from "../../../../../typings/types/common";
import { Operation } from "../../../../../typings/types/operation";
import { getAbnormalOperationListAPI } from "../../api/operatingRisk";
import { ZCC } from "../../../../utils/BasePage";
import { formatNumber } from "../../../../utils/util";

// subPackages/packageA/operatingRisk/abnormal.ts
interface PageData {
  companyId: string
}
new ZCC.RefreshListPage<any>({

  async getListDatas() {
    const that = this
    this.setRefreshing()
    try {
      const res = await getAbnormalOperationListAPI(this.data.companyId, this.data.page)
      if (res.content) {
        console.log(res.content)
        let tmp = this.processListDatas(res.content)
        that.updateDatasWithNewItems(tmp, res.content.total, res.content.hasNextPage)
      }
    } finally {
      that.endRefreshing()
    }
  },
  processListDatas(content: ZCC_TYPE.ListResult<Operation.Abnormal>) {

    const cells = content.list.flatMap((item: Operation.Abnormal, index: number) => {

      let tmp = [{
        idx: formatNumber(index + 1),
        status: 'add',
        id: item.id,
        hasRemove: item.removeDecisionOffice && item.removeDecisionOffice.length > 0,//25/3/6临时修改
        rows: [
          { title: '列入经营异常名录原因：', val: item.addReason, ishorizontal: false },
          { title: '作出决定机关（列入）：', val: item.decisionOffice, ishorizontal: true },
          { title: '列入日期：', val: item.addDate, ishorizontal: true },
        ]
      }]
      if (item.removeDecisionOffice && item.removeDecisionOffice.length > 0) {
        tmp.push({
          idx: formatNumber(index + 1),
          status: 'remove',
          id: item.id,
          hasRemove: true,
          rows: [
            { title: '移出经营异常名录原因：', val: item.removeReason, ishorizontal: false },
            { title: '作出决定机关（移出）：', val: item.removeDecisionOffice, ishorizontal: true },
            { title: '移出日期：', val: item.removeDate, ishorizontal: true },
          ]
        })
      }
      return tmp
    })
    return cells
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    companyId: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      companyId: this.options.companyId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getListDatas()
  },

  onPullDownRefresh() {
    if (this.resetPageIfNeed()) {
      this.getListDatas()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getListDatas()
  },
})